package com.ft.admix.bd.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;

import com.baidu.mobads.sdk.api.RewardVideoAd;
import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsRewardVideoAd;
import com.ft.admix.ad.MixCustomRewardVideoAdapter;
import com.ft.admix.common.MixConstant;
import com.ft.admix.config.AdConfig;
import com.ft.admix.model.MixRewardItem;
import com.ft.admix.utils.ThreadUtils;
import com.kwad.sdk.api.KsScene;
import com.kwad.sdk.api.KsVideoPlayConfig;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class RewardAdapterLoader extends MixCustomRewardVideoAdapter {

    RewardVideoAd mRewardAd;

    @Override
    protected void loadNetworkAd(Activity context, AdConfig config) {
//        if (!Bds.haseInit()||KsAdSDK.getLoadManager() == null) {
//            callLoadFailed(-1, "快手SDK未初始化");
//            return;
//        }
        mRewardAd = new RewardVideoAd(context, config.getAdId(), new RewardVideoAd.RewardVideoAdListener() {
            @Override
            public void onAdShow() {
                callRewardVideoAdShow();
            }

            @Override
            public void onAdClick() {
                callRewardVideoAdClick();
            }

            @Override
            public void onAdClose(float v) {
                callRewardVideoAdClose();
            }

            @Override
            public void onAdFailed(String s) {
                callLoadFailed(20001, s);
            }

            @Override
            public void onVideoDownloadSuccess() {

            }

            @Override
            public void onVideoDownloadFailed() {

            }

            @Override
            public void playCompletion() {
                callRewardVideoComplete();
            }

            @Override
            public void onAdLoaded() {
                callLoadSuccess();
            }

            @Override
            public void onAdSkip(float v) {
                callRewardVideoAdSkipped();
            }

            @Override
            public void onRewardVerify(boolean b) {
                if (b) {
                    callRewardVideoRewardVerify(null);
                }
            }
        });
        mRewardAd.load();
    }

    @Override
    public MixConstant.AdReadyStatus isReadyCondition() {
        /**
         * 在子线程中进行广告是否可用的判断
         */
        Future<MixConstant.AdReadyStatus> future = ThreadUtils.runOnThreadPool(new Callable<MixConstant.AdReadyStatus>() {
            @Override
            public MixConstant.AdReadyStatus call() throws Exception {
                if (mRewardAd != null && mRewardAd.isReady()) {
                    return MixConstant.AdReadyStatus.AD_IS_READY;
                } else {
                    return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
                }
            }
        });
        try {
            MixConstant.AdReadyStatus result = future.get(500, TimeUnit.MILLISECONDS);
            if (result != null) {
                return result;
            } else {
                return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return MixConstant.AdReadyStatus.AD_IS_NOT_READY;
    }

    @Override
    public void show(Activity activity) {
        if (mRewardAd != null) {
            mRewardAd.show(activity);
        }
    }
} 